package com.mmsj;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * @author ChuXuan
 */
public class MyService extends Service {
    Handler myHandler;
    Runnable myRunnable;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("Mylog", "创建服务······");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        Log.d("Mylog", "启动服务······");
//        return super.onStartCommand(intent, flags, startId);
        // 定义一个计时器 每隔2s输出日志
       myHandler = new Handler();
       myRunnable = new Runnable() {
           @Override
           public void run() {
               Log.d("Mylog", "运行服务······");
               myHandler.postDelayed(this, 2000);
           }
       };
       // 开启定时器
        myHandler.postDelayed(myRunnable, 2000);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {

        // 关闭定时器
        myHandler.removeCallbacks(myRunnable);
        super.onDestroy();
        Log.d("Mylog", "销毁服务······");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
